package xq;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;

import javax.swing.JPanel;

class Chess // 棋子类
{
	public static final short REDPLAYER = 1;
	public static final short BLACKPLAYER = 0;
	public short player; // 红子为REDPLAYER，黑子为BLACKPLAYER
	public String typeName; // 帅、士...
	public Point pos; // 位置
	private Image chessImage; // 棋子图案

	public Chess(short player, String typeName, Point chesspos) {
		this.player = player;
		this.typeName = typeName;
		this.pos = chesspos;
		// 初始化棋子图案
		// 不能使用switch (typeName)因为switch不支持String枚举
		if (player == REDPLAYER) {
			if (typeName.equals("帅"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/帅.png");
			else if (typeName.equals("仕"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/士.png");
			else if (typeName.equals("相"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/相.png");
			else if (typeName.equals("马"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq马.png");
			else if (typeName.equals("车"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/车.png");
			else if (typeName.equals("炮"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/炮.png");
			else if (typeName.equals("兵"))
				chessImage = Toolkit.getDefaultToolkit().getImage("xq/兵.png");
		} else// 黑方棋子
		{
			if (typeName.equals("将"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/将1.png");
			else if (typeName.equals("士"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/仕1.png");
			else if (typeName.equals("象"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/象1.png");
			else if (typeName.equals("马"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/马1.png");
			else if (typeName.equals("车"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/车1.png");
			else if (typeName.equals("炮"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/炮1.png");
			else if (typeName.equals("卒"))
				chessImage = Toolkit.getDefaultToolkit()
						.getImage("xq/卒1.png");
		}
	}

	public void SetPos(int x, int y) // 设置棋子位置
	{
		pos.x = x;
		pos.y = y;

	}

	public void ReversePos() // 棋子位置对调
	{
		pos.x = 10 - pos.x;
		pos.y = 11 - pos.y;
	}

//	public void Draw(Graphics g) // 绘制自己到棋盘上
//	{
//		g.drawImage(chessImage, pos.x * 40 - 40, pos.y * 40 - 40, 40, 40, null);
//	}


	// 指定的JPanel上画棋子
	protected void paint(Graphics g, JPanel i) {
		g.drawImage(chessImage, pos.x * 40 - 40, pos.y * 40 - 20, 40, 40,
				(ImageObserver) i);
	}

	public void DrawSelectedChess(Graphics g) {
		// 画选中棋子的示意边框线
		g.drawRect(pos.x * 40 - 40, pos.y * 40 - 20, 40, 40);
	}


}